package com.huawei.hianalytics.f.g;

import com.huawei.hianalytics.g.b;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class g {

    public static class a extends Exception {
        public a() {
            super("Fail to encrypt with RSA");
        }
    }

    public static String a(String str, String str2) {
        String str3 = "";
        try {
            str3 = b(str, str2);
        } catch (a e) {
            b.c("RsaCryPter", "rsaEncrypt(): Fail to encrypt with RSA!");
        }
        return str3;
    }

    private static PublicKey a(String str) {
        return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(f.a(str)));
    }

    private static byte[] a(String str, byte[] bArr) {
        try {
            Key a = a(str);
            if (a == null) {
                throw new UnsupportedEncodingException();
            }
            Cipher instance = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING");
            instance.init(1, a);
            return instance.doFinal(bArr);
        } catch (UnsupportedEncodingException e) {
            b.c("RsaCryPter", "rsaEncrypt(): getBytes - Unsupported coding format!");
            throw new a();
        } catch (NoSuchAlgorithmException e2) {
            b.c("RsaCryPter", "rsaEncrypt(): getInstance - No such algorithm,transformation");
            throw new a();
        } catch (InvalidKeyException e3) {
            b.c("RsaCryPter", "rsaEncrypt(): init - Invalid key!");
            throw new a();
        } catch (NoSuchPaddingException e4) {
            b.c("RsaCryPter", "rsaEncrypt():  No such filling parameters ");
            throw new a();
        } catch (BadPaddingException e5) {
            b.c("RsaCryPter", "rsaEncrypt():False filling parameters!");
            throw new a();
        } catch (InvalidKeySpecException e6) {
            b.c("RsaCryPter", "rsaEncrypt(): Invalid key specification");
            throw new a();
        } catch (IllegalBlockSizeException e7) {
            b.c("RsaCryPter", "rsaEncrypt(): doFinal - The provided block is not filled with");
            throw new a();
        }
    }

    private static String b(String str, String str2) {
        try {
            return f.a(a(str, str2.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            b.c("RsaCryPter", "rsaEncrypt(): Unsupported Encoding - utf-8!");
            throw new a();
        }
    }
}
